package cn.stylefeng.guns.modular.basic.controller;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.model.params.ArchiveLayerParam;
import cn.stylefeng.guns.modular.basic.model.result.ArchiveLayerResult;
import cn.stylefeng.guns.modular.basic.service.ArchiveLayerService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.guns.utils.ServiceAssert;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 档案层信息控制器
 *
 * @author blesslp
 * @Date 2019-11-17 10:01:29
 */
@Controller
@RequestMapping("/archiveLayer")
public class ArchiveLayerController extends BaseController {

    private String PREFIX = "/basic/archiveLayer";

    @Autowired
    private ArchiveLayerService archiveLayerService;

    /**
     * 跳转到主页面
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/archiveLayer.html";
    }

    /**
     * 新增页面
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/archiveLayer_add.html";
    }

    /**
     * 编辑页面
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/archiveLayer_edit.html";
    }

    /**
     * 新增接口
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ArchiveLayerParam archiveLayerParam) {
        this.archiveLayerService.add(archiveLayerParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ArchiveLayerParam archiveLayerParam) {
        this.archiveLayerService.update(archiveLayerParam);
        return ResponseData.success();
    }

    @Autowired private UserService userService;
    /**
     * 删除接口
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ArchiveLayerParam archiveCabinetParam) {
        Long userId = ShiroKit.getUserNotNull().getId();
        User user = this.userService.getById(userId);

        String oldMd5 = ShiroKit.md5(archiveCabinetParam.getPwd(), user.getSalt());

        if (user.getPassword().equals(oldMd5)) {
            //密码比对成功
            this.archiveLayerService.delete(archiveCabinetParam);
            return ResponseData.success();
        }
        ServiceAssert.assetTrue(false, "密码错误");
        return ResponseData.success();
    }

    /**
     * 检测是否含有档案
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/check")
    @ResponseBody
    public ResponseData check(ArchiveLayerParam archiveBoxParam) {
        final boolean hasArchiveInfo = this.archiveLayerService.hasArchiveInfo(archiveBoxParam.getId());
        ServiceAssert.assetFalse(hasArchiveInfo,"");
        this.archiveLayerService.delete(archiveBoxParam);
        return ResponseData.success(hasArchiveInfo);
    }


    /**
     * 查看详情接口
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ArchiveLayerParam archiveLayerParam) {
        final ArchiveLayerResult bySpec = this.archiveLayerService.findBySpec(archiveLayerParam);
        return ResponseData.success(bySpec);
    }

    /**
     * 查询列表
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ArchiveLayerParam archiveLayerParam) {
        return this.archiveLayerService.findPageBySpec(archiveLayerParam);
    }


    @ResponseBody
    @RequestMapping("/tree")
    public List<ZTreeNode> tree(ArchiveLayerParam param) {
        List<ZTreeNode> tree = this.archiveLayerService.tree(param);
        tree.add(ZTreeNode.createParent());
        return tree;
    }
    @RequestMapping("/getMaxCode")
    @ResponseBody
    public ResponseData getMaxCode(@RequestParam(value = "ssdasid",required = true)String ssdasid,@RequestParam(value = "ssdagid",required = true)String ssdagid){
        String maxCode = this.archiveLayerService.getMaxCode(ssdasid,ssdagid);
        return ResponseData.success(StrUtil.isNotBlank(maxCode) ? maxCode : 1);
    }

}


